#version 460

uniform int area;
uniform int coef_n;
uniform int kernel_w, kernel_h;

layout(location = 0) out vec4 FragColor;
in vec2 TexCoord;

layout(binding = 0) uniform sampler2D diffuse;

// layout(rgba32f, binding = 0) uniform image2D radiance_map;
layout(rgba32f, binding = 1) uniform image2D radiance_map_after_haar;
// layout(rgba32f, binding = 2) uniform image2D radiance_map_after_sss;

layout(std430, binding = 0) buffer KernelCoef
{
	vec4 data[];
} kernel_coef;

void main() {
	int row = int(TexCoord.y * kernel_h);
	int col = int(TexCoord.x * kernel_w);
	vec3 sum = vec3(0, 0, 0);
	int index_texel = row * kernel_w + col;
	for (int i = 0; i < coef_n; i++)
	{
		float weight = kernel_coef.data[index_texel * coef_n + i].x;
		if (weight == 0.0)
			break;
		int index = int(kernel_coef.data[index_texel * coef_n + i].y);
		int row_t = index / kernel_w;
		int col_t = index % kernel_w;
		sum += imageLoad(radiance_map_after_haar, ivec2(col_t, row_t)).rgb * weight;
	}
	FragColor = vec4(sum, 1.0);
	// FragColor = vec4(kernel_coef.data[index_texel * coef_n].x != 0 ? 1.0 : 0.0, 0, 0, 1.0);
}
